﻿using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class MulFenHuSBFP : BasePage
    {
        #region 属性
        public DataTable DtFenHuSB
        {
            get
            {
                if (Session["DtFenHuSB"] == null)
                {
                    Session["DtFenHuSB"] = new DataTable();
                }
                return Session["DtFenHuSB"] as DataTable;
            }
            set { Session["DtFenHuSB"] = value; }
        }
        private string YuanYongHuH
        {
            get
            {
                return Session["YuanYongHuH"] == null ? null : Session["YuanYongHuH"].ToString();
            }
            set { Session["YuanYongHuH"] = value; }
        }

        #endregion

        #region 变量

        SpHelper _sphelper = new SpHelper();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                SetBtnStatus();
            }
        }
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            if (e.Parameter == "setinfo")
            {
                string msg = "";
                int index = gvFenHu.FocusedRowIndex;
                if (index >= 0)
                {
                    msg = string.Join("|", gvFenHu.GetRowValues(index, "S_SHUIBIAOBH").ToString(), 
                        Convert.ToString(gvFenHu.GetRowValues(index, "S_SHUIBIAOMC").ToString()), 
                        Convert.ToString(gvFenHu.GetRowValues(index, "S_YONGHUBH").ToString()));
                    e.Result = e.Parameter + "|0|" + msg;
                }
                else
                {
                    e.Result = e.Parameter + "|-1|" + msg;
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string shuibiaobh = txtShuiBiaoBH.Text;
            string fenhuh = cmbFenHuH.Value == null ? "" : cmbFenHuH.Value.ToString();
            if (DtFenHuSB == null || DtFenHuSB.Rows.Count == 0)
            {
                return;
            }
            //更新到列表中
            foreach (DataRow dr in DtFenHuSB.Rows)
            {
                if (shuibiaobh == dr["S_SHUIBIAOBH"].ToString())
                {
                    dr["S_YONGHUBH"] = fenhuh;
                }
            }
            gvFenHu.DataSource = this.DtFenHuSB;
            gvFenHu.DataBind();
            gvFenHu.FocusedRowIndex = -1;
            gvFenHu.FocusedRowIndex = 0;
        }

        private void InitControl()
        {
            string YongHuH = Request["YongHuH"] == null ? "" : Request["YongHuH"];
            DataTable dtFenHuList = (DataTable)Session["dtFenHuList"];
            if (YongHuH.Trim().Length == 0 || dtFenHuList == null || dtFenHuList.Rows.Count==0)
            {
                PageTools.AlertMsg(this, "参数用户号错误", "window.close();");
                return;
            }
            //绑定分户号
            for (int i = 0; i < dtFenHuList.Rows.Count; i++)
            {
                cmbFenHuH.Items.Add(dtFenHuList.Rows[i]["S_YONGHUH"].ToString(), dtFenHuList.Rows[i]["S_YONGHUH"].ToString());
            }
            //绑定列表
            if (string.IsNullOrEmpty(this.Request["laihanid"]))
            {
                if(this.YuanYongHuH != Request["YONGHUH"])
                {
                    DtFenHuSB = _sphelper.P_YSLH_GETFENHUSHUIBIAO(YongHuH, null).Tables[0];
                    this.YuanYongHuH = Request["YONGHUH"];
                }
            }
            else
            {
                DtFenHuSB = _sphelper.P_YSLH_GETFENHUSHUIBIAO(YongHuH, Convert.ToInt32(Request["laihanid"])).Tables[0];
            }
            if (DtFenHuSB.Rows.Count == 0)
            {
                PageTools.AlertMsg(this, "分户用户无相关水表信息", "window.close();");
                return;
            }
            gvFenHu.DataSource = this.DtFenHuSB;
            gvFenHu.DataBind();
            gvFenHu.FocusedRowIndex = -1;
            gvFenHu.FocusedRowIndex = 0;
        }
        private void SetBtnStatus()
        {
            bool b = Request["type"] != "1";
            cmbFenHuH.Enabled = b;
            BtnSave.Visible = b;
        }
    }
}